<template>
  <view class="pay">
    <!-- 1 收货地址 -->
    <view class="address u-p-20">
      <view class="address-btn u-flex u-row-center" v-if="!addressInfo">
        <u-button
          shape="circle"
          type="error"
          plain
          size="medium"
          :custom-style="{ backgroundColor: '#fff!important' }"
          @click="handleChooseAddress"
          >获取收货地址</u-button
        >
      </view>

      <view class="address-detail" v-else>
        <view class="address-detail">{{detail}}</view>
        <view class="address-user">{{addressInfo.userName}}{{addressInfo.telNumber}}</view>
      </view>
    </view>

    <u-line color="#eee"></u-line>

    <!-- 2 已选商品 -->
    <view class="goods-list u-p-15">
      <view class="goods-list-title">已选商品</view>
      <view class="goods-list-content">
        <view class="cart-item"  
        v-for="item in checkedCartList"
        :key="item.goods_id">
          <!-- 2 图片 -->
          <view class="goods-img">
            <u-image width="191rpx" height="191rpx">{{item.goods_small_logo}}</u-image>
          </view>
          <!-- 3 信息 -->
          <view class="goods-info">
            <!-- 名称 -->
            <view class="goods-name u-line-2">{{item.goods_name}}</view>
            <!-- 价格和数量 -->
            <view class="goods-price-num">
              <view class="goods-price">￥{{item.goods_price}}</view>
              <view class="goods-num-tool">
                <view class="goods-vnum">X{{item.num}}</view>
              </view>
            </view>
          </view>
        </view>
      </view>
    </view>

    <!-- 2 统计 -->
    <view class="statistics">
      <view class="all-price"> 合计: <text>￥{{totalPrice}}</text> </view>
      <view class="all-count">
        <u-button type="error" shape="circle"  @click="handlePay">去支付({{totalCount}})</u-button>
      </view>
    </view>
  </view>
</template>

<script>
import {  mapGetters } from "vuex";
export default {

  data() {
    return {
      //收货地址信息
      addressInfo:null
    }
  },
  computed:{
    ...mapGetters('cart',["checkedCartList","totalCount","totalPrice"]),
    
    detail(){
      if(!this.addressInfo) return ''
      const { provinceName, cityName, countyName, detailInfo } =
      this.addressInfo;
      return  provinceName +  cityName + countyName + detailInfo
    }
  },
  methods: {
     async handleChooseAddress(){
      const [err,addressInfo] = await uni.chooseAddress();
      if(err){
        console.log(err);
      }else{
        this.addressInfo = addressInfo
        console.log(this.addressInfo);
      }
    },
    // 商品支付  
   async handlePay(){
    // 判断是否有收货地址
   if(!this.addressInfo){
    uni.showToast({
     title:'您还没有选择收货地址',
     icon:'none'
    })
     return;
   }
    
  
    //  1,获取用户信息 通过调用api
    const  [err,res1] = await uni.getUserProfile({desc:"用来给用户提供更好的服务"})
    const {encryptedData,rawData,iv,signature} = res1
    //2,根据uni.login获取凭证code,用于后台生成对应的token
    const [err2,res2] = await uni.login()
    console.log(99,res2);
    const {code} = res2
    // 3, 获取用户token
     const res3 = await this.$u.http.post("/users/wxlogin",
     {encryptedData,rawData,iv,signature,code})
     console.log(104,res3);

     const {token} = res3;
    // 4,  创建订单
    // 4.1 构造请求体参数
      const  orderParams = {
      order_price:this.totalPrice,
      consignee_addr:this.detail,
       goods:this.checkedCartList.map(item=>({
       goods_id:item.goods_id,
       goods_number: item.num,
       goods_price:item.goods_price
      })),
      }
     // 4.2 发请求
      const res4 = await this.$u.http.post('/my/orders/create',orderParams,{Authorization:token})
      console.log(120,res4);

      const {order_number} = res4 
     // 5 通过订单编号获取订单参数
     const  res5 = await  this.$u.http.post("/my/orders/req_unifiedorder",{order_number},{Authorization:token})
     console.log(125,res5);
     const {pay} = res5

    // 6,开始调用微信支付
      uni.requestPayment(pay)
    }




   }
};
</script>

<style lang="scss">
.pay {
  padding-bottom: 111rpx;
}
.cart-item {
  display: flex;
  padding: 10rpx;
  border-bottom: 1rpx solid #ccc;

  .goods-img {
    flex: 2;

    display: flex;
    align-items: center;
    justify-content: center;
  }

  .goods-info {
    color: #666;
    flex: 3;
    display: flex;
    flex-direction: column;
    justify-content: space-around;
    .goods-name {
    }

    .goods-price-num {
      display: flex;
      justify-content: space-between;
      .goods-price {
        color: $theme-color;
      }

      .goods-num-tool {
        width: 140rpx;
        height: 40rpx;

        display: flex;
        justify-content: flex-end;

        .goods-vnum {
        }
      }
    }
  }
}
.statistics {
  position: fixed;
  background-color: #fff;
  bottom: 0;
  left: 0;
  width: 100%;
  z-index: 100;
  border-top: 1rpx solid #ccc;
  display: flex;
  align-items: center;
  padding: 15rpx;
  .all-price {
    flex: 1;
    color: #666;
    text {
    }
  }

  .all-count {
  }
}
</style>